<?php
    class Default_Model_News extends Zend_Db_Table_Abstract
    {
        protected $_db;
		
		public function __construct()
		{
			$this->_db = Zend_Registry::get('db');
		}
        
        public function listItem($locale)
        {
            $news = array(
                'n.id',
                'n.image'
            );
            $news_language = array(
                'nl.title',
                'nl.description',
            );
            $sql = $this->_db->select()
                            ->from('news AS n', $news)
                            ->join('news_language AS nl', 'nl.news_id = n.id', $news_language)
                            ->join('language AS l', 'l.id = nl.language_id', null)
                            ->where('n.status = 1')
                            ->where('l.locale = ?', $locale)
                            ->order('n.id DESC');
            return $sql;
        }
        
        public function getItem($id, $locale)
        {
            $news = array(
                'category_id',
                'create_date',
            );
            $news_language = array(
                'title',
                'description',
            );
            $sql = $this->_db->select()
                            ->from('news AS n', $news)
                            ->join('news_language AS nl', 'nl.news_id = n.id', $news_language)
                            ->join('language AS l', 'l.id = nl.language_id', null)
                            ->where('n.id = ?', $id)
                            ->where('n.status = 1')
                            ->where('l.locale = ?', $locale);
            $result = $this->_db->fetchRow($sql);
            return $result;
        }
        
        public function getItemByCategory($data, $locale)
        {
            $news = array(
                'n.id',
                'n.image'
            );
            $news_language = array(
                'nl.title',
                'nl.description',
            );
            $sql = $this->_db->select()
                            ->from('news AS n', $news)
                            ->join('news_language AS nl', 'nl.news_id = n.id', $news_language)
                            ->join('language AS l', 'l.id = nl.language_id', null)
                            ->where('n.category_id = ?', $data['category_id'])
                            ->where('n.status = 1')
                            ->where('l.locale = ?', $locale);
            return $sql;
        }

        public function getItemSimilar($data, $locale)
        {
            $news = array(
                'n.id'
            );
            $news_language = array(
                'nl.title'
            );
            $sql = $this->_db->select()
                            ->from('news AS n', $news)
                            ->join('news_language AS nl', 'nl.news_id = n.id', $news_language)
                            ->join('language AS l', 'l.id = nl.language_id', null)
                            ->where('n.id <> ?', $data['id'])
                            ->where('n.category_id = ?', $data['category_id'])
                            ->where('n.status = 1')
                            ->where('l.locale = ?', $locale)
                            ->limit(5);
            $result = $this->_db->fetchAll($sql);
            return $result;
        }
    }
